package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersPageQueryDTO1;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.entity.User;
import com.sky.exception.BaseException;
import com.sky.mapper.DetailMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.result.PageResult;
import com.sky.service.AddressBookService;
import com.sky.service.OrderService;
import com.sky.service.ShoppingCarService;
import com.sky.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.xmlbeans.impl.soap.Detail;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private ShoppingCarService shoppingCarService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private DetailMapper detailMapper;
    @Autowired
    private ShoppingCarService carService;


    /**
     * 下订单
     * */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public OrderSubmitVO subimt(OrdersSubmitDTO dto) {
       User user= userService.getById(BaseContext.getCurrentId());
       if(user==null){
           throw new BaseException("用户名不能为空");
       }
        //addressBookId查询收货地址
        AddressBook addressBook = addressBookService.getById(dto.getAddressBookId());
        if(addressBook==null){
            throw new BaseException("收货地址不能为空");
        }



        //查询我的购物车，得到菜品或者套餐数据
        List<ShoppingCartVO> list = shoppingCarService.list();
        if(CollectionUtils.isEmpty(list)){
            throw new BaseException("购物车不能为空");
        }
        //生成订单
        //insert orders
        Orders orders = new Orders();
        BeanUtils.copyProperties(dto,orders);
        orders.setNumber(UUID.randomUUID().toString());
        orders.setStatus(Orders.PENDING_PAYMENT);
        orders.setUserId(BaseContext.getCurrentId());
        orders.setUserName(user.getName());
        orders.setOrderTime(LocalDateTime.now());
        orders.setPayStatus(Orders.UN_PAID);
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getProvinceName()+addressBook.getCityName()+addressBook.getDistrictName()+addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());
        ordersMapper.insert(orders);


        //insert order_detail
        ArrayList<OrderDetail> orderDetails = new ArrayList<>();
        for (ShoppingCartVO shoppingCartVO: list){
            OrderDetail detail = new OrderDetail();
            BeanUtils.copyProperties(shoppingCartVO,detail);
            detail.setId(null);
            detail.setOrderId(orders.getId());
            orderDetails.add(detail);
        }
        detailMapper.insertBatch(orderDetails);
        //清空购物车
        carService.clean();
        OrderSubmitVO vo = new OrderSubmitVO();
        vo.setId(orders.getId());
        vo.setOrderTime(orders.getOrderTime());
        vo.setOrderAmount(orders.getAmount());
        vo.setOrderNumber(orders.getNumber());
        return vo;
    }
    /**
     * 订单搜索
     * */

    @Override
    public PageResult<OrdersPageVO> page(OrdersPageQueryDTO1 ordersPageQueryDTO1) {
        //TOOD最后一个字段没查
        PageHelper.startPage(ordersPageQueryDTO1.getPage(),ordersPageQueryDTO1.getPageSize());
        Page<OrdersPageVO> page= ordersMapper.page(ordersPageQueryDTO1);
        return new PageResult<>(page.getTotal(),page.getResult());


    }
    /**
     * 查看订单详情
     * */

    @Override
    public OrderChaKanVO chakan(Integer id) {
        //TOOD plus不会用就不连表了
        OrderChaKanVO vo= ordersMapper.selectById(id);
        return vo;
    }

    /***
     * 查看各个订单的数量
     *
     */

    @Override
    public OrderStatusVO status() {

        Integer i3=3;
        //待派送
        // Integer con= ordersMapper.selectByStatus(i2);
        Integer con1 =ordersMapper.selectByStatus(i3);


        //派送中
        Integer i4=4;
        Integer de= ordersMapper.selectByStatus(i4);
        //待接单
        Integer i2=2;

        Integer to=   ordersMapper.selectByStatus(i2);
        OrderStatusVO vo = new OrderStatusVO();
        vo.setConfirmed(con1);
        vo.setDeliveryInProgress(de);
        vo.setToBeConfirmed(to);
        return vo;


    }
}
